//
//  WebViewController.m
//  HarmonizePub
//
//  Created by rzhy on 2016/10/20.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "WebViewController.h"
#import "Config.h"
#import <NJKWebViewProgressView.h>
#import <NJKWebViewProgress.h>

@interface WebViewController () <UIWebViewDelegate, NJKWebViewProgressDelegate>
{
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
}
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UIBarButtonItem *leftItem  = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    nagetiveSpacer.width = -15;
    self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
    
    [self createProgress];
    if (self.url) {
        NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
        [self.webView loadRequest:request];
    }
    self.webView.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (_webViewProgressView) {
        [_webViewProgressView removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网页返回方法
- (void)onClickedOKbtn {
//    if ([self.webView canGoBack]) {
//        [self.webView goBack];
//    } else {
//        [self.view resignFirstResponder];
//        [self.navigationController popViewControllerAnimated:YES];
//    }
    [self.view resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [storage cookies])
        [storage deleteCookie:cookie];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (!webView.userInteractionEnabled) webView.userInteractionEnabled = YES;
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    if (title.length) self.title = title;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    XHLog(@"error: %@", error.localizedDescription)
    [ZYToast showBottomText:@"加载失败！"];
    _webViewProgressView.hidden = YES;
}


#pragma mark - Progress
- (void)createProgress {
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width, 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
}

#pragma mark - NJKWebViewProgress Delegate
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress {
    [_webViewProgressView setProgress:progress animated:YES];
}

@end

@implementation NSURLRequest(ForSSL)

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
    return YES;
}

@end
